<?php

namespace App\Http\Controllers\Spider;

use App\Http\Controllers\Cache\CacheController;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;

class SpiderController extends Controller
{
    private $webcache;

    public function __construct()
    {
        $this->webcache = new CacheController();

    }

    protected function Collect($url, $rules,$range="")
    {
        if (Cache::has($url)) {
            $html = Cache::get($url);
            if ($html == "") {
                $html = $this->GetHtml($url);
            }
        } else {
            $html = $this->GetHtml($url);
            $this->webcache->HtmlCache($url, $html);
        }
        $data = $this->QList()->html($html)->rules($rules)->range($range)->query()->getData()->all();
        $this->QList()->destruct();
        return $data;
    }

    private function GetHtml($url)
    {
        $html = $this->Snoopy()->fetch($url)->results;
        return $html;
    }

    protected function GetTvData($url, $type)
    {
        if(config('thread.thread')==1){
            return $this->MulitGetTvData($url,$type);
        }
        $arr = explode('/', $url);
        $id = str_replace('.html', '', $arr[2]);
        $arr = ['youku' => '优酷视频', 'qq' => '腾讯视频', 'imgo' => '芒果TV', 'qiyi' => '爱奇艺', 'levp' => '乐视视频', 'cntv' => 'CNTV', 'sohu' => '搜狐视频', 'tudou' => '土豆视频', 'pptv' => 'PPTV'];
        $jg = [];
        foreach ($arr as $key => $v) {
            $api = 'https://www.360kan.com/cover/switchsite?site=' . $key . '&id=' . $id . '&category=' . $type;
            $res = json_decode($this->Common()->curl_get($api), true);
            if ($res['error'] == 0) {
                $sp['name'] = $v;
                $sp['data'] = $this->CleanHtml($res['data']);
                $jg[] = $sp;
            }
        }
        return $jg;
    }
    
    private function MulitGetTvData($url,$type){
        $arr = explode('/', $url);
        $id = str_replace('.html', '', $arr[2]);
        $arr = ['youku' => '优酷视频', 'qq' => '腾讯视频', 'imgo' => '芒果TV', 'qiyi' => '爱奇艺', 'levp' => '乐视视频', 'cntv' => 'CNTV', 'sohu' => '搜狐视频', 'tudou' => '土豆视频', 'pptv' => 'PPTV'];
        $jg = [];
        foreach ($arr as $key=>$v){
            $api[] = 'https://www.360kan.com/cover/switchsite?site=' . $key . '&id=' . $id . '&category=' . $type;
            $video[] = $v;
        }
        $res =$this->Common()->curl_http($api,15);
      foreach ($res as $k=>$j){
          if($j['error']==0){
              $sp['name'] = $video[$k];
              $sp['data'] = $this->CleanHtml($j['data']);
              $jg[] = $sp;
          }
      }
     return $jg;
    }

    private function CleanHtml($html)
    {
        $str = stripslashes($html);
        $str = str_replace(["\r", "\\"], "", $str);
        $rule = $this->GetRules('rules', '360kan', 'tvlist');
        $data = $this->QList()->html($str)->rules($rule)->query()->getData()->all();
        return $data;
    }

    protected function PostApi($key,$zyku=null,$zyid=null){
        if(config('thread.privatekey')){
            $api = laracvs('services.api');
            if(!empty($key)){
                $res = $this->common()->curl_post($api,['wd'=>$key,'privatekey'=>config('thread.privatekey')]);
            }else{
                $arr = ['zyku'=>$zyku,'zyid'=>$zyid,'type'=>'play','privatekey'=>config('thread.privatekey')];
                $res = $this->common()->curl_post($api,$arr);
            }
            return json_decode($res,1);
        }

    }

}